# Run python setup.py to generate wheel file, rename wheel file and copy it to install path.
 
# Set the variable passed from configure_file in package.cmake/package_fc.cmake
set(GIT_HASH "@GIT_HASH@")
set(Python3_EXECUTABLE @Python3_EXECUTABLE@)
set(PYTHON_LIBPATH  @PYTHON_LIBPATH@)
set(DATASYSTEM_WHEEL_PATH @DATASYSTEM_WHEEL_PATH@)
set(DATASYSTEM_SETUP_PATH @DATASYSTEM_SETUP_PATH@)
set(CMAKE_INSTALL_PATH @ARG_CMAKE_INSTALL_PATH@)
set(ENV{COMMIT_ID} "${GIT_HASH}")
 
# Copy all libs file and exclude sym file.
message("cmake install prefix: ${CMAKE_INSTALL_PREFIX}")
if(EXISTS ${PYTHON_LIBPATH})
    file(COPY ${PYTHON_LIBPATH}/
         DESTINATION ${DATASYSTEM_WHEEL_PATH}/lib
         REGEX ".*sym$" EXCLUDE)
endif()
file(GLOB_RECURSE FILE_LIST ${CMAKE_INSTALL_PREFIX}/sdk/cpp/lib/*so*)
file(WRITE "${DATASYSTEM_WHEEL_PATH}/sdk_lib_list" "")
foreach(FILE ${FILE_LIST})
    get_filename_component(FILENAME ${FILE} NAME)
    file(APPEND "${DATASYSTEM_WHEEL_PATH}/sdk_lib_list" "${FILENAME}\n")
endforeach()

# Run python setup.py bdist_whell to generate origin wheel file.
execute_process(COMMAND ${Python3_EXECUTABLE} setup.py bdist_wheel
        WORKING_DIRECTORY ${DATASYSTEM_SETUP_PATH})
file(GLOB ORIGIN_WHEEL "dist/dist/*.whl")


file(COPY ${ORIGIN_WHEEL} DESTINATION ${CMAKE_INSTALL_PATH}/)
# Remove temporary package path
file(REMOVE_RECURSE ${DATASYSTEM_SETUP_PATH})